import RNFetchBlob from 'react-native-fetch-blob'

export const fsReadPath = async (remoteUrl) => {
    const dirs = RNFetchBlob.fs.dirs
    const FILE_PATH = `${dirs.DocumentDir}/${remoteUrl.replace(/https:\/\/|http:\/\//, '')}`
    const result = await RNFetchBlob.fs.exists(FILE_PATH)

    //从本地读取到了该文件，直接返回路径
    if (result == false) {
        //console.log('load from remote')
        let data = await RNFetchBlob.fetch('GET', remoteUrl)
        await RNFetchBlob.fs.writeFile(FILE_PATH, data.data, 'base64')
    } else {
         //console.log('load from local')
    }

    return FILE_PATH
}


export const dirName = () => {
    const dirs = RNFetchBlob.fs.dirs
    return `${dirs.DocumentDir}/${App.awsEndPoint}/${App.awsBucketName}/${current_user.login_name}`
}